package com.ruoyi.web.util.QyWx;

import com.ruoyi.common.constant.Constants;
import org.apache.commons.collections.list.CursorableLinkedList;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Scanner;


/**
 * 发送企业微信的工具类
 * @author zhangwenqiang
 */
public class SendQyWxMsgUtils {


    //消息体
    //拼接json字符串
    //可以进行其他的拓展 比如卡片形式、图片形式等
    public static String sendText(String content,String partCode,String partBatchCode,String partDescription,String url) throws IOException {
        // 获取报告地址
        String reportUrl = content;
        // 获取零件名称
        String name = getName(content);
        content = String.format(
                "{\n" +
                        "  \"msgtype\": \"markdown\",\n" +
                        "  \"markdown\": {\n" +
                        "    \"content\": \"您有一条新的待审批检测的报告\\n批次号: %s \\n零件编码: %s\\n零件名称: %s\\n零件规格: %s\\n状态: 待审批\\n检测报告: [查看报告](%s)<@%s>\",\n" +
                        "  }\n" +
                        "}",
                partBatchCode,partCode,name,partDescription, reportUrl, "廖勇"
        );

        return send(content,url);
    }

    public static String sendText(String content,String partBatchCode,String partCode,String partDescription,String assignee,String url) throws IOException {
        // 获取报告地址
        String reportUrl = content;
        // 获取零件名称
        String name = getName(content);
        content = String.format(
                "{\n" +
                        "  \"msgtype\": \"markdown\",\n" +
                        "  \"markdown\": {\n" +
                        "    \"content\": \"您有一条新的待审批检测的报告\\n批次号: %s \\n零件编码: %s\\n零件名称: %s\\n零件规格: %s\\n状态: 待审批\\n检测报告: [查看报告](%s)<@%s>\",\n" +
                        "  }\n" +
                        "}",
                partBatchCode,partCode,name, partDescription,reportUrl, assignee
        );

        return send(content,url);
    }

    /**
     * 发送审核完的通知
     * @param content
     * @param url
     * @return
     * @throws IOException
     */
    public static String sendFinishText(String content,String partBatchCode,String partCode,String partDescription,String url) throws IOException {
        // 获取报告地址
        String reportUrl = content;
        // 获取零件名称
        String name = getName(content);
        content = String.format(
                "{\n" +
                        "  \"msgtype\": \"markdown\",\n" +
                        "  \"markdown\": {\n" +
                        "    \"content\": \"零件已审批合格，请及时处理入库！\\n批次号: %s \\n零件编码: %s\\n零件名称: %s\\n零件规格: %s\\n状态: 审批通过\\n检测报告: [查看报告](%s)<@%s><@%s><@%s><@%s>\",\n" +
                        "  }\n" +
                        "}",
                partBatchCode,partCode,name,partDescription, reportUrl, Constants.WZBZ, Constants.PTY_1, Constants.PTY_2,Constants.RKY
        );

        return send(content,url);
    }

    public static String sendReturnText(String content,String partBatchCode,String partCode,String partDescription,String url) throws IOException {
        // 获取报告地址
        String reportUrl = content;
        // 获取零件名称
        String name = getName(content);
        content = String.format(
                "{\n" +
                        "  \"msgtype\": \"markdown\",\n" +
                        "  \"markdown\": {\n" +
                        "    \"content\": \"零件审批不合格，请及时办理退货！\\n批次号: %s \\n零件编码: %s\\n零件名称: %s\\n零件规格: %s\\n状态: 审批不通过\\n检测报告: [查看报告](%s)<@%s><@%s><@%s><@%s>\",\n" +
                        "  }\n" +
                        "}",
                partBatchCode,partCode,name,partDescription, reportUrl, Constants.WZBZ, Constants.PTY_1, Constants.PTY_2,Constants.RKY
        );

        return send(content,url);
    }

    /**
     * 发送降级通知
     * @param content
     * @param url
     * @return
     * @throws IOException
     */
    public static String sendPriceReductionText(String content,String partBatchCode,String partCode,String partDescription,String url) throws IOException {

        // 获取报告地址
        String reportUrl = content;
        // 获取零件名称
        String name = getName(content);
        content = String.format(
                "{\n" +
                        "  \"msgtype\": \"markdown\",\n" +
                        "  \"markdown\": {\n" +
                        "    \"content\": \"零件审批通过，请按降级处理入库，并做好黄牌标识！\\n批次号: %s \\n零件编码: %s\\n零件名称: %s\\n零件规格: %s\\n状态: 审批通过\\n检测报告: [查看报告](%s)<@%s><@%s><@%s><@%s><@%s>\",\n" +
                        "  }\n" +
                        "}",
                partBatchCode,partCode,name,partDescription, reportUrl, Constants.CWBZ, Constants.PTY_1, Constants.PTY_2, Constants.WZBZ,Constants.RKY
        );

        return send(content,url);
    }

    /**
     * 推送未审核完流程
     * @param content
     * @param url
     * @return
     * @throws IOException
     */
    public static String sendUrgeApprove(String content, String url) throws IOException {
        content = "{"
                + "\"msgtype\": \"text\","
                + "\"text\": {"
                + "\"content\": \"当前还有"+ content + "条未审核完的报告，请及时处理！" + "\","
                + "\"mentioned_mobile_list\": [ \"@all\"]"
                + "}"
                + "}";
        return send(content, url);
    }

    /**
     * 推送每日审核完结果汇总
     * @return
     */
    public static String sendTotalApprove(String total,String ok,String no,String url) throws IOException {
        String content = "" +
                "{\n" +
                "    \"msgtype\": \"markdown\",\n" +
                "    \"markdown\": {\n" +
                "        \"content\": \"今日审批完报告数量合计：<font color=\\\"warning\\\">"+ total + "条" + "</font>" +
                "\\n>审批合格:<font color=\\\"comment\\\">"+ ok + "条" +"</font>" +
                "\\n>审核不合格:<font color=\\\"comment\\\">" + no + "条" +"</font>\"\n" +
                "    }\n" +
                "}";

        return send(content,url);
    }

    /**
     * 发送通知的方法
     * @param textMsg
     * @param url
     * @return
     * @throws IOException
     */
    public static String send(String textMsg,String url) throws IOException {
        //实例化对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //发送请求
        HttpPost httpPost = new HttpPost(url);
        //发送消息的格式;
        httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
        //编码转换
        StringEntity se = new StringEntity(textMsg, "utf-8");
        httpPost.setEntity(se);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //发送成功接收返回值
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println("发送微信机器人消息成功 " + result);
            return result;
        } else {
            System.out.println("发送微信机器人消息失败");
        }
        // 关闭
        httpClient.close();
        response.close();
        return "发送微信机器人消息失败";
    }

    /**
     * 获取检测报告的零件名称
     * @param content
     * @return
     */
    private static String getName(String content) {
        // 找到 ".pdf" 的位置
        int pdfIndex = content.indexOf(".pdf");
        // 找到最后一个下划线的位置
        int lastUnderscoreIndex = content.lastIndexOf("_");

        // 提取 ".pdf" 和最后一个下划线之间的部分
        String result = content.substring(lastUnderscoreIndex + 1, pdfIndex);
        return result;
    }
}
